//
// Created by xzy.xiao on 2022/6/8.
//

#include "MyMainWindow.h"
#include <iostream>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QMessageBox>
#include <QLabel>
#include <QStatusBar>
#include <QFileDialog>
#include <QInputDialog>
#include <QList>

MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent) {
    initMenuAndToolBar();
    initStatusBar();
}

MyMainWindow::~MyMainWindow() = default;

void MyMainWindow::openFileSlot() {

    QString path = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files(*.jpg *.png)"));
    if (path.length() == 0) {
        QMessageBox::information(nullptr, tr("Path"), tr("You didn't select any files."));
    } else {
        QMessageBox::information(nullptr, tr("Path"), tr("You selected：") + path);
    }
}

void MyMainWindow::testMessageBoxSlot() {
    QMessageBox::StandardButton clickedButton = QMessageBox::information(nullptr, "Test", "Test QMessageBox",
                                                                         QMessageBox::Yes | QMessageBox::No,
                                                                         QMessageBox::Yes);
    if (clickedButton == QMessageBox::Yes) {
        QMessageBox::about(nullptr, "Clicked result", "You clicked <font color = blue> YES </font> button");
    } else {
        QMessageBox::about(nullptr, "Clicked result", "You clicked <font color = red> No </font> button");
    }
}

void MyMainWindow::testInputDialogSlot() {
    bool isOk;
    QString inputStr = QInputDialog::getText(nullptr, "Test QInputDialog", "Please input something", QLineEdit::Normal,
                                             "input here", &isOk);
    if (isOk) {
        QMessageBox::information(nullptr, "Show input result", "You input is：" + inputStr);
    }
}

/*====================SUPPORTER====================*/

/**
 * 初始化：菜单、工具栏
 */
void MyMainWindow::initMenuAndToolBar() {
    // 1.创建Action
    openFileActionPtr = new QAction(tr("&Open"), this);
    openFileActionPtr->setShortcut(QKeySequence::Open); // 快捷键
    openFileActionPtr->setStatusTip(tr("Open a file.")); // 提示语句
    openFileActionPtr->setIcon(QIcon(":/images/folder-open.png")); // 图标

    testMessageBoxPtr = new QAction(tr("&MessageBox"), this);
    testMessageBoxPtr->setStatusTip("Test QMessageBox.");
    testMessageBoxPtr->setIcon(QIcon(":/images/tom-icon.png"));

    testInputDialogPtr = new QAction(tr("&InputDialog"));
    testInputDialogPtr->setStatusTip("Test QInputDialog.");
    testInputDialogPtr->setIcon(QIcon(":/images/xiaoxin-icon.png"));

    // 2.设置事件响应
    connect(openFileActionPtr, SIGNAL(triggered(bool)), this, SLOT(openFileSlot()));
    connect(testMessageBoxPtr, SIGNAL(triggered(bool)), this, SLOT(testMessageBoxSlot()));
    connect(testInputDialogPtr, SIGNAL(triggered(bool)), this, SLOT(testInputDialogSlot()));

    // 3.添加到菜单、工具栏
    QMenuBar *menuBarPtr = menuBar();
    menuBarPtr->addMenu(tr("&File"))->addAction(openFileActionPtr);

    QList<QAction *> *testActions = new QList<QAction *>();
    testActions->append(testMessageBoxPtr);
    testActions->append(testInputDialogPtr);
    menuBarPtr->addMenu(tr("&Test"))->addActions(*testActions);

    addToolBar(tr("&File"))
            ->addAction(openFileActionPtr);
    addToolBar(tr("&Test"))
            ->addAction(testMessageBoxPtr);
    addToolBar(tr("&Test"))
            ->addAction(testInputDialogPtr);
}

/**
 * 初始化：状态栏
 */
void MyMainWindow::initStatusBar() {
    QStatusBar *statusBarPtr = statusBar();
    msgLabelPtr = new QLabel();
    msgLabelPtr->setMinimumSize(msgLabelPtr->sizeHint());
    msgLabelPtr->setAlignment(Qt::AlignHCenter);
    statusBarPtr->addWidget(msgLabelPtr);
    statusBarPtr->setStyleSheet(QString("QStatusBar::item{border: 0px}"));
}
